package com.example.dnfgamercommunitybackend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.dnfgamercommunitybackend.dao.ArticleDao;
import com.example.dnfgamercommunitybackend.dao.ArticleLikesDao;
import com.example.dnfgamercommunitybackend.entity.Article;
import com.example.dnfgamercommunitybackend.entity.ArticleLikes;
import com.example.dnfgamercommunitybackend.entity.ResponseResult;
import com.example.dnfgamercommunitybackend.service.ArticleService;
import com.example.dnfgamercommunitybackend.service.CommentService;
import com.example.dnfgamercommunitybackend.util.DfaUtil;
import com.example.dnfgamercommunitybackend.util.JwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleDao articleDao;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private CommentService commentService;
    @Resource
    private DfaUtil dfaUtil;
    @Resource
    private ArticleLikesDao articleLikesDao;

    @Override
    public String addArticle(Article article,String token){
        if ("".equals(article.getTitle())
                || "".equals(article.getContent())){
            return JSONObject.toJSONString(ResponseResult.failure("文章信息不能为空"));
        }else {
            //过滤文章，检查是否合规
            if (dfaUtil.textFilter(article.getTitle())){
                if (dfaUtil.textFilter(article.getContent())){
                    int accountId = Integer.parseInt(jwtUtil.getUserIdFromToken(token));
                    article.setAccountId(accountId);
                    int result = articleDao.insertArticle(article);
                    if (result>0){
                        return JSONObject.toJSONString(ResponseResult.success("文章添加成功"));
                    }else {
                        return JSONObject.toJSONString(ResponseResult.failure("文章添加失败"));
                    }
                }else {
                    return ResponseResult.failureStringResult(505,"文章内容不合规，请修改");
                }
            }else {
                return ResponseResult.failureStringResult(505,"文章标题不合规，请修改");
            }
        }
    }

    @Override
    //通过id删除文章
    public String deleteArticleById(int id) {
        //如果id为空，返回失败
        if (String.valueOf(id).isEmpty()){
            return ResponseResult.failureStringResult(504,"删除失败，未选择文章");
        }
        //id不为空，继续
        int deleteResult = articleDao.deleteArticleById(id);
        if(deleteResult > 0){
            return ResponseResult.successStringResult("删除成功");
        }
        return JSONObject.toJSONString(ResponseResult.failure("删除失败"));
    }

    @Override
    public String updateArticle(Article article, String authorization) {
        //文章id为空，返回更新失败
        if (!StringUtils.hasText(article.getTitle())
                || !StringUtils.hasText(article.getContent())){
            return ResponseResult.failureStringResult(504,"更新失败,标题或内容为空。");
        }else {
            //过滤文章，检查是否合规
            if (dfaUtil.textFilter(article.getTitle())){
                if (dfaUtil.textFilter(article.getContent())){
                    //获取用户id
                    int userId = Integer.parseInt(jwtUtil.getUserIdFromToken(authorization));
                    article.setAccountId(userId);
                    if (articleDao.updateArticle(article) > 0){
                        return ResponseResult.successStringResult("更新成功");
                    }else{
                        return ResponseResult.failureStringResult("更新失败,文章id不存在");
                    }
                }else {
                    return ResponseResult.failureStringResult(505,"文章内容不合规，请修改");
                }
            }else {
                return ResponseResult.failureStringResult(505,"文章标题不合规，请修改");
            }
        }
    }

    @Override
    public String getArticleDetail(int articleId,String authorization) {
        Article article = articleDao.getArticleDetail(articleId);
        if (Objects.isNull(article)){
            return ResponseResult.failureStringResult("文章id不存在");
        }else {
            //获取用户id
            int userId = Integer.parseInt(jwtUtil.getUserIdFromToken(authorization));
            ArticleLikes articleLikes = new ArticleLikes();
            articleLikes.setArticleId(articleId);
            articleLikes.setAccountId(userId);
            article.setLikeStatus(!Objects.isNull(articleLikesDao.existRecord(articleLikes)));
            article.setComments(commentService.getAllCommentByArticleId(article.getId()));
            return ResponseResult.successStringResult("响应成功",article);
        }
    }

    @Override
    public String getAllArticle() {
        List<Article> articles = articleDao.getAllArticle();
        if (articles.isEmpty()){
            return ResponseResult.failureStringResult("响应失败");
        }
        return ResponseResult.successStringResult("响应成功",articles);
    }

    @Override
    public String getUserArticleByUserId(String authorization) {
        List<Article> articles = articleDao.getUserArticleByUserId(Integer.parseInt(jwtUtil.getUserIdFromToken(authorization)));
        if (articles.isEmpty()){
            return ResponseResult.failureStringResult(504,"暂无个人文章");
        }
        return ResponseResult.successStringResult("获取文章列表成功",articles);

    }

    @Override
    public String getArticleByKeyword(String keyword) {
        //检查接收参数是否为空
        if (StringUtils.hasText(keyword)){
            List<Article> articles = articleDao.getArticleByKeyword(keyword);
            if (!articles.isEmpty()){
                return ResponseResult.successStringResult("搜索成功",articles);
            }
            return ResponseResult.failureStringResult(505,"暂无相关文章");
        }
        return ResponseResult.failureStringResult(504,"关键字为空");
    }
}
